﻿/***************
 * Copyright 2009 (C) 
 * Timur Fanshteyn
 * Blog: http://blog.tfanshteyn.com
 * ************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Coherence.Linq
{
    internal class ExpressionTreeModifier<TData> : ExpressionVisitor
    {
        private IQueryable<TData> queryablePlaces;

        internal ExpressionTreeModifier(IQueryable<TData> data)
        {
            this.queryablePlaces = data;
        }

        internal Expression CopyAndModify(Expression expression)
        {
            return this.Visit(expression);
        }

        protected override Expression VisitConstant(ConstantExpression c)
        {
            // Replace the constant QueryableTerraServerData arg with the queryable Place collection.
            if (c.Type == typeof(CoherenceQuery<TData>))
                return Expression.Constant(this.queryablePlaces);
            else
                return c;
        }
    }
}
